//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.xw.dao.SysTypeMapper;
import com.xw.vo.TypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SysTypeServiceImpl implements SysTypeService {
    @Autowired
    private SysTypeMapper typeMapper;

    @Override
    public List<TypeVo> findAll() {
        List<TypeVo> list = typeMapper.findAll();
        Multimap<String,TypeVo> multimap = ArrayListMultimap.create();
        for (TypeVo type : list) {
            multimap.put(type.getStFid(),type);
        }
        List<TypeVo> typeList = (List<TypeVo>) multimap.get("0");
        recursion(multimap, typeList);
        return typeList;
    }

    private void recursion(Multimap<String, TypeVo> multimap, List<TypeVo> typeList) {
        for (TypeVo typeVo : typeList) {
            List<TypeVo> typeVos = (List<TypeVo>) multimap.get(typeVo.getStId());
            typeVo.setList(typeVos);
            recursion(multimap,typeVos);
        }
    }
}
